package com.xh3140.xwidget.button;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.CountDownTimer;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.Locale;

@SuppressWarnings("unused")
public class CountdownButton extends Button {
  /**
   * 倒计时时间 s
   */
  private int mCountdown;

  /**
   * 没有倒计时的时候显示的文本
   */
  private CharSequence mNormalText;

  /**
   * 倒计时中显示文本的格式
   * 其中须含有一个“%d”，用来显示倒计时
   */
  private String mTimerTextFormat;

  /**
   * 秒级计时器
   */
  private SecondTimer mSecondTimer;

  public CountdownButton(@NonNull Context context) {
    this(context, null);
  }

  public CountdownButton(@NonNull Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, androidx.appcompat.R.attr.buttonStyle);
  }

  public CountdownButton(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CountdownButton);
    try {
      mCountdown = a.getInt(R.styleable.CountdownButton_countdown, mCountdown);
      mNormalText = a.getText(R.styleable.CountdownButton_normalText);
      mTimerTextFormat = a.getString(R.styleable.CountdownButton_timerTextFormat);
    } finally {
      super.setText(mNormalText);
      a.recycle();
    }
  }

  public void startTimer() {
    if (mCountdown <= 0) {
      return;
    }
    if (mSecondTimer != null) {
      mSecondTimer.onFinish();
      mSecondTimer.cancel();
    }
    mSecondTimer = new SecondTimer(mCountdown);
    mSecondTimer.start();
  }

  public void cancelTimer() {
    if (mSecondTimer != null) {
      mSecondTimer.onFinish();
      mSecondTimer.cancel();
    }
  }

  @Override
  protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    cancelTimer();
  }

  public int getCountdown() {
    return mCountdown;
  }

  public void setCountdown(int countdown) {
    mCountdown = countdown;
  }

  @Nullable
  public CharSequence getNormalText() {
    return mNormalText;
  }

  public void setNormalText(@Nullable CharSequence normalText) {
    mNormalText = normalText;
    super.setText(mNormalText);
  }

  @Nullable
  public String getTimerTextFormat() {
    return mTimerTextFormat;
  }

  public void setTimerTextFormat(@Nullable String timerTextFormat) {
    mTimerTextFormat = timerTextFormat;
  }

  private class SecondTimer extends CountDownTimer {

    public SecondTimer(int secondsInFuture) {
      super(secondsInFuture * 1000L, 1000L);
    }

    @Override
    public void onTick(long millisUntilFinished) {
      setEnabled(false);
      if (mTimerTextFormat != null) {
        setText(String.format(Locale.getDefault(), mTimerTextFormat, (int) Math.ceil(1e-3 * millisUntilFinished)));
      } else {
        setText(String.format(Locale.getDefault(), "%s(%ds)", mNormalText, (int) Math.ceil(1e-3 * millisUntilFinished)));
      }
    }

    @Override
    public void onFinish() {
      setEnabled(true);
      setText(mNormalText);
    }
  }
}
